//Begin of App definition and implementation.
var businesspartner = {};
(function($) {
	//Test local storage
	businesspartner.Item = Backbone.Model.extend({});
	businesspartner.Items = Backbone.Collection.extend({
		model: businesspartner.Item,
		url:"http://localhost/WebContent/puppet_rest/work.php/partners",
		dataType: "json",
		crossDomain:true,
		comparator: function(item) {
			return item.get("firstName");
		},
		parse: function(response) {
			cleanJSON = JSON.parse(JSON.stringify(response));
			console.log(cleanJSON);
			return cleanJSON;
		},
	});
	businesspartner.ItemListView = Backbone.View.extend({
		tagName: "ul",
		id: "businesspartner-list",
		attributes: {
			"data-role": "listview",
			"data-inset": "true",
			"data-filter": "true",
			"data-filter-placeholder": "Search your connections",
			"data-dividertheme": "a"
		},
		initialize: function() {
			this.collection.bind("add", this.add, this);
			this.collection.bind("change", this.changeItem, this);
			this.collection.bind("reset", this.render, this);
			this.template = Handlebars.compile($("#businesspartner-list-item-template").html());
		},
		render: function() {
			var container = this.options.viewContainer,
				businesspartners = this.collection,
				template = this.template,
				listDividerValue = new Array(),
				listView = $(this.el);
			$(this.el).empty();
			businesspartners.each(function(businesspartner) {
				console.log(businesspartner);
				var renderedItem = template(businesspartner.toJSON()),
					$renderedItem = $(renderedItem);

				//To get the list divider of Alphabets				
				businesspartner.get("firstName")[0] === -1 ? listDividerValue.push(businesspartner.get("firstName")[0]) : listView.append("<li data-role='list-divider'>" + businesspartner.get("firstName")[0] + "</li>");
				//End of list divider of Alphabet implementation
				$renderedItem.jqmData("businesspartnerid", businesspartner.get("id"));
				$renderedItem.bind("click", function() {
					$("#businesspartner-details").jqmData("businesspartnerid", $(this).jqmData("businesspartnerid"));
				});
				listView.append($renderedItem);
			});
			container.html($(this.el));
			container.trigger("create");
			return this;
		},
		add: function(item) {
			var businesspartnerList = $("#businesspartner-list"),
				template = this.template;
			businesspartnerList.append(template(item.toJSON()));
			businesspartnerList.listview("refresh")
		},
		changeItem: function(item) {
			this.collection.sort();
		}
	});

	businesspartner.ItemDetailsView = Backbone.View.extend({
		initialize: function() {
			this.template = Handlebars.compile($("#businesspartner-details-template").html());
		},
		render: function() {
			var container = this.options.viewContainer,
				item = this.model,
				renderedContent = this.template(item.toJSON());
			container.html(renderedContent);
			container.trigger("create");
			return this;
		}
	});
	businesspartner.ItemFormView = Backbone.View.extend({
		attributes: {
			"data-role": "fieldcontain"
		},
		initialize: function() {
			this.template = Handlebars.compile($("#businesspartner-form-template").html());
		},
		render: function() {
			var container = this.options.viewContainer,
				item = this.model,
				renderedContent = this.template(item.toJSON());
			container.html(renderedContent);
			container.trigger("create");
			return this;
		}
	});
	businesspartner.initData = function() {
		businesspartner.items = new businesspartner.Items;
		businesspartner.items.fetch({
			async: false
		});
	}
})(jQuery);
$("#BusinessPartner").live("pageinit", function(event) {
	var businesspartnerListContainer = $("#BusinessPartner").find(":jqmData(role='content')"),
		businesspartnerListView;
	businesspartner.initData();
	businesspartnerListView = new businesspartner.ItemListView({
		collection: businesspartner.items,
		viewContainer: businesspartnerListContainer
	});
	businesspartnerListView.render()
});
$("#businesspartner-details").live("pagebeforeshow", function() {
	var itemDetailsContainer = $("#businesspartner-details").find(":jqmData(role='content')"),
		itemDetailsView, itemId = $("#businesspartner-details").jqmData("businesspartnerid"),
		itemModel = businesspartner.items.get(itemId);
	itemDetailsView = new businesspartner.ItemDetailsView({
		model: itemModel,
		viewContainer: itemDetailsContainer
	});
	itemDetailsView.render();
});
$("#add-button").live("click", function() {
	var item = new businesspartner.Item,
		itemForm = $("#businesspartner-form-form"),
		businesspartnerFormView;
	$("#businesspartner-details").jqmRemoveData("itemId");
	businesspartnerFormView = new businesspartner.ItemFormView({
		model: item,
		viewContainer: itemForm
	});
	businesspartnerFormView.render();
});
$("#edit-button").live("click", function() {
	var itemId = $("#businesspartner-details").jqmData("businesspartnerid"),
		itemModel = businesspartner.items.get(itemId),
		itemForm = $("#businesspartner-form-form"),
		businesspartnerFormView;
	businesspartnerFormView = new businesspartner.ItemFormView({
		model: itemModel,
		viewContainer: itemForm
	});
	businesspartnerFormView.render();
});
$("#save-button").live("click", function() {
	var itemId = $("#businesspartner-details").jqmData("businesspartnerid"),
		formval = $('#businesspartner-form-form');
		formDataAll = $(formval).toObject({mode: "combine"});
		fromJSON = formDataAll;

	if (itemId) 
	{
		item = businesspartner.items.get(itemId);
		item.set(fromJSON);
	} else {
		item = new businesspartner.Item(fromJSON);
		item.set({
			"id": (new Date).getTime()
		});
		businesspartner.Items.add(item);
	}
	item.save();
	console.log(JSON.stringify(item));
});

function save_data()
{
	/* Dummy variable for purposes of testing till Praveen does a connect to check
	network availability
	*/
	var nwAvailability = true;
	if(nwAvailability)
	{

	}	
	else
	{

	}
}